package com.wfh.mianshiji.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wfh.mianshiji.common.ErrorCode;
import com.wfh.mianshiji.constant.UserConstant;
import com.wfh.mianshiji.exception.BusinessException;
import com.wfh.mianshiji.model.entity.User;
import com.baomidou.mybatisplus.extension.service.IService;
import com.wfh.mianshiji.model.vo.UserVo;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

/**
* @author fenghuanwang
* @description 针对表【user(用户表)】的数据库操作Service
* @createDate 2025-10-30 15:36:18
*/
public interface UserService extends IService<User> {

    /**
     * 获取当前登录用户
     *
     * @param request
     * @return
     */
    default UserVo getCurrentUser(HttpServletRequest request){
        Object attribute = request.getSession().getAttribute(UserConstant.USER_LOGIN_STATE);
        UserVo userVo = (UserVo) attribute;
        if (userVo == null){
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        return userVo;
    }

    /**
     * 查询用户包装类
     *
     * @param userQuery
     * @return
     */
    default LambdaQueryWrapper<User> getQueryWrapper(Object userQuery) {
        User user = BeanUtil.copyProperties(userQuery, User.class);
        Long id = user.getId();
        String userAccount = user.getUserAccount();
        String userPassword = user.getUserPassword();
        String userName = user.getUserName();
        String userProfile = user.getUserProfile();
        String userRole = user.getUserRole();
        return Wrappers.lambdaQuery(User.class)
                .eq(ObjectUtils.isNotEmpty(id), User::getId, id)
                .eq(StringUtils.isNotEmpty(userAccount), User::getUserAccount, userAccount)
                .like(StringUtils.isNotEmpty(userName), User::getUserName, userName)
                .like(StringUtils.isNotEmpty(userProfile), User::getUserProfile, userPassword)
                .eq(StringUtils.isNotEmpty(userRole), User::getUserRole, userRole);
    }
}
